﻿using System.Collections.Generic;
using System;
using CommunityToolkit.Mvvm;
using CommunityToolkit.Mvvm.ComponentModel;
using LiveCharts;
using System.Linq;
using System.Windows.Input;
using CommunityToolkit.Mvvm.Input;

namespace AdvanceChartApp
{
    internal class MainViewModel : ObservableObject
    {
        List<DataModel> _dataModels = new List<DataModel>();

        private ChartValues<DataModel> _seriesValues;
        private ChartValues<DataModel> _seriesValues2;
        private ChartValues<DataModel> _seriesValues3;
        private List<double> _seriesLabels;

        public double MinValue
        {
            get { return _minValue; }
            set
            {
                OnChartChange(_minValue, value);
                SetProperty(ref _minValue, value);
            }
        }

        private void OnChartChange(double minValue, double value)
        {
            if (minValue > value) //左移
            { }
            else //右移
            { }
        }

        public double MaxValue
        {
            get { return _maxValue; }
            set { SetProperty(ref _maxValue, value); }
        }
        public ChartValues<DataModel> SeriesValues
        {
            get { return _seriesValues; }
            set { SetProperty(ref _seriesValues, value); }
        }

        public ChartValues<DataModel> SeriesValues2
        {
            get { return _seriesValues2; }
            set { SetProperty(ref _seriesValues2, value); }
        }

        public ChartValues<DataModel> SeriesValues3
        {
            get { return _seriesValues3; }
            set { SetProperty(ref _seriesValues3, value); }
        }

        public Func<double, string> XFormatter { get; set; }
        public Func<double, string> YFormatter { get; set; }

        public ICommand BtnCmd
        {
            get =>
                new RelayCommand<object>(o =>
                {
                    MaxValue = (_dataModels.Last().Time - default(DateTime)).TotalMinutes;
                    MinValue = MaxValue - 120;
                });
        }

        public MainViewModel()
        {
            // Generate sample data
            SeriesValues = new ChartValues<DataModel>();
            SeriesValues2 = new ChartValues<DataModel>();
            SeriesValues3 = new ChartValues<DataModel>();

            XFormatter = x =>
                x >= 0 ? default(DateTime).AddMinutes(x).ToString("MM-dd HH:mm") : "00-00 00:00";
            DateTime dateTime = new DateTime(2024, 3, 21);
            Random random = new Random();
            while (dateTime < DateTime.Now)
            {
                _dataModels.Add(new DataModel { Value = random.Next(100, 1000), Time = dateTime });
                dateTime = dateTime.AddMinutes(1);
            }
            SeriesValues.AddRange(_dataModels);
            SeriesValues2.AddRange(_dataModels);
            SeriesValues3.AddRange(_dataModels);
            //MaxValue = (_dataModels.Last().Time - default(DateTime)).TotalMinutes;
            //MinValue = MaxValue - 120;
            MaxScrollBarValue = _dataModels.Count - 120;
            ScrollBarValue = MaxScrollBarValue;
        }

        private int _scrollBarValue;
        private ChartValues<double> _chartValues;
        private List<string> _labels;
        private double _minValue;
        private double _maxValue;

        public int ScrollBarValue
        {
            get { return _scrollBarValue; }
            set
            {
                _scrollBarValue = value;
                OnPropertyChanged("ScrollBarValue");
                UpdateChartData(value);
            }
        }

        public void UpdateChartData(int startIndex)
        {
            // Update chart values and labels based on the startIndex
            SeriesValues.Clear();
            SeriesValues2.Clear();
            SeriesValues3.Clear();

            for (int i = startIndex; i < startIndex + 120; i++)
            {
                SeriesValues.Add(_dataModels[i]); // Sample data
                SeriesValues2.Add(_dataModels[i]); // Labels
                SeriesValues3.Add(_dataModels[i]); // Labels
            }
        }

        public int MaxScrollBarValue { get; set; }
    }
}
